//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package com.marakana.obolo;

import database.DBAdapter;
import game.Human;

import game.IAHard;
import game.Obolo;
import game.Player;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class StoryCreatorActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	protected void onResume() {
		super.onResume();
		// Crea els obolos per instanciarlos durant la historia
		Obolo ob1, ob2, ob3, ob4, ob5, ob6, ob7, ob8, ob9, ob0;
		// Moment de la historia on es troba
		
		DBAdapter dbPlayer = new DBAdapter(this);
		dbPlayer.open();
		int estat = dbPlayer.getPlayer(1).getInt(5);
		dbPlayer.close();
		
		String[] rules = null;
		String[] storyText = null;
		int[] dimension = new int[3];

		// Crea les dimensions del taulell i els jugadors
		dimension[0] = 3;
		dimension[1] = 3;
		dimension[2] = 0;
		Player user = new Human("USER");
		Player rival = new IAHard("PORC");

		/*
		 * SharedPreferences prefs = PreferenceManager
		 * .getDefaultSharedPreferences(this); int estat = prefs.getInt("story",
		 * 0);
		 */

		switch (estat) {
		case 0:
			ob1 = new Obolo(new String[] { "4", "7", "3", "6" });
			ob2 = new Obolo(new String[] { "5", "1", "1", "8" });
			ob3 = new Obolo(new String[] { "6", "6", "3", "3" });
			ob4 = new Obolo(new String[] { "7", "2", "4", "7" });
			ob5 = new Obolo(new String[] { "1", "6", "9", "3" });
			user.setDeck(new Obolo[] { ob1, ob2, ob3, ob4, ob5 });
			ob6 = new Obolo(new String[] { "7", "5", "3", "7" });
			ob7 = new Obolo(new String[] { "9", "6", "4", "1" });
			ob8 = new Obolo(new String[] { "1", "4", "5", "5" });
			ob9 = new Obolo(new String[] { "6", "4", "2", "4" });
			ob0 = new Obolo(new String[] { "4", "6", "3", "7" });
			rival.setDeck(new Obolo[] { ob6, ob7, ob8, ob9, ob0 });
			storyText = getResources().getStringArray(R.array.strStoryStep0);
			rules = new String[] { "upward" };
			break;

		case 1:
			ob1 = new Obolo(new String[] { "sword", "sword", "4", "6" });
			ob2 = new Obolo(new String[] { "4", "shield", "5", "7" });
			ob3 = new Obolo(new String[] { "4", "5", "6", "7" });
			ob4 = new Obolo(new String[] { "1", "1", "7", "8" });
			ob5 = new Obolo(new String[] { "shield", "sword", "7", "8" });
			user.setDeck(new Obolo[] { ob1, ob2, ob3, ob4, ob5 });
			ob6 = new Obolo(new String[] { "shield", "shield", "1", "9" });
			ob7 = new Obolo(new String[] { "sword", "4", "6", "7" });
			ob8 = new Obolo(new String[] { "2", "1", "8", "9" });
			ob9 = new Obolo(new String[] { "4", "4", "6", "7" });
			ob0 = new Obolo(new String[] { "7", "7", "7", "7" });
			rival.setDeck(new Obolo[] { ob6, ob7, ob8, ob9, ob0 });
			storyText = getResources().getStringArray(R.array.strStoryStep1);
			rules = new String[] { "downward" };
			break;

		case 2:
			ob1 = new Obolo(new String[] { "4", "6", "8", "2" });
			ob2 = new Obolo(new String[] { "7", "sword", "3", "7" });
			ob3 = new Obolo(new String[] { "6", "3", "6", "3" });
			ob4 = new Obolo(new String[] { "7", "7", "shield", "1" });
			ob5 = new Obolo(new String[] { "1", "7", "7", "7" });
			user.setDeck(new Obolo[] { ob1, ob2, ob3, ob4, ob5 });
			ob6 = new Obolo(new String[] { "4", "5", "6", "7" });
			ob7 = new Obolo(new String[] { "3", "8", "4", "7" });
			ob8 = new Obolo(new String[] { "7", "shield", "7", "7" });
			ob9 = new Obolo(new String[] { "7", "7", "sword", "7" });
			ob0 = new Obolo(new String[] { "3", "3", "5", "7" });
			rival.setDeck(new Obolo[] { ob6, ob7, ob8, ob9, ob0 });
			storyText = getResources().getStringArray(R.array.strStoryStep2);
			rules = new String[] { "sum" };
			break;

		case 3:
            ob1 = new Obolo(new String[] { "4", "7", "5", "7" });
            ob2 = new Obolo(new String[] { "5", "5", "6", "6" });
            ob3 = new Obolo(new String[] { "4", "5", "6", "7" });
            ob4 = new Obolo(new String[] { "8", "3", "7", "5" });
            ob5 = new Obolo(new String[] { "4", "sword", "7", "7" });
            user.setDeck(new Obolo[] { ob1, ob2, ob3, ob4, ob5 });
            ob6 = new Obolo(new String[] { "6", "6", "7", "7" });
            ob7 = new Obolo(new String[] { "4", "8", "8", "5" });
            ob8 = new Obolo(new String[] { "7", "7", "9", "5" });
            ob9 = new Obolo(new String[] { "6", "9", "2", "7" });
            ob0 = new Obolo(new String[] { "9", "7", "5", "9" });
            rival.setDeck(new Obolo[] { ob6, ob7, ob8, ob9, ob0 });
            storyText = getResources().getStringArray(R.array.strStoryStep3);
            rules = new String[] { "sum", "equal", "chain" };
            break;
            
		case 4:
            ob1 = new Obolo(new String[] { "2", "6", "7", "4" });
            ob2 = new Obolo(new String[] { "sword", "sword", "sword", "sword" });
            ob3 = new Obolo(new String[] { "7", "shield", "shield", "7" });
            ob4 = new Obolo(new String[] { "8", "7", "6", "7" });
            ob5 = new Obolo(new String[] { "9", "9", "6", "3" });
            user.setDeck(new Obolo[] { ob1, ob2, ob3, ob4, ob5 });
            ob6 = new Obolo(new String[] { "shield", "shield", "shield", "shield" });
            ob7 = new Obolo(new String[] { "8", "8", "7", "7" });
            ob8 = new Obolo(new String[] { "9", "3", "7", "4" });
            ob9 = new Obolo(new String[] { "5", "5", "8", "9" });
            ob0 = new Obolo(new String[] { "sword", "sword", "9", "4" });
            rival.setDeck(new Obolo[] { ob6, ob7, ob8, ob9, ob0 });
            storyText = getResources().getStringArray(R.array.strStoryStep4);
            rules = new String[] { "sum", "equal", "chain" };
            break;
		default:
			Dialog storyDialog = new Dialog(this);
			storyDialog.setOwnerActivity(this);
			storyDialog.setTitle("PORC");
			storyDialog.setCancelable(true);
			storyDialog.setContentView(R.layout.story_dialog);
			TextView txtDialeg = (TextView) storyDialog.findViewById(R.id.txtDialogStory);
			txtDialeg.setText("Felicitats! T'has passat el mode historia, posa't a prova contra les bèsties de l'infern, si tens OBOLOS!");
			TextView txtContinue = (TextView) storyDialog.findViewById(R.id.txtContinueStory);
						
			txtContinue.setText(R.string.txtContinueStory);
			txtContinue.setOnTouchListener(new OnTouchListener(){
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					finish();
					return false;
				}
			});
			
			break;
		}
		
		if(estat < 5){
			Intent newStoryGame = new Intent(this, StoryActivity.class);

			newStoryGame.putExtra("user", user);
			newStoryGame.putExtra("rival", rival);
			newStoryGame.putExtra("storytext", storyText);
			newStoryGame.putExtra("rules", rules);
			newStoryGame.putExtra("dimension", dimension);

			// startActivityForResult(newStoryGame, RESULT_OK);
			startActivity(newStoryGame);
			
			finish();
		}
		
	}
}
